﻿var pageObject = {
    requestId: '',   
    total: 0,
    listWrap: $('#listWrap'),
    pagerUrl: 'XXX.asmx/GetList',
    pagerPamater: {
        searchKey: '',
        currentPage: 1,
        pageSize: 20
    },
    preBtn: $('#preBtn'),
    nextBtn: $('#nextBtn'),
    addDialogBtn: $('#addDialog'),
    closeDialogBtn: $('#cancle'),
    submitBtn: $('#submitBtn'),
    submitPamater: {},
    addPamater: {},
    addSubmitUrl: 'XXX.asmx/Submit',
    modifyShowDialogBtn: null,
    modfiyPamater: {},
    modifySubmitUrl: 'XXX.asmx/Submit',
    getDetailUrl: '',
    isModify:false,
    initPageObject: function () {
        pageJs.requestId = this.requestId;
        this.initDialog();
        this.preBtn.live('click', pageObject.getPrePage);//上一页事件
        this.nextBtn.live('click', pageObject.getNextPage);//下一页事件
        this.closeDialogBtn.live('click', pageObject.closeDialog);
        this.getPagerList();//由于要绑定编辑按钮事件，所以列表初始化放在前面
        this.addDialogBtn.on('click', pageObject.initAddDialog);//添加对话框展示      
        this.submitBtn.one('click', pageObject.infoSubmit);//数据提交
        this.closeDialogBtn.live('click', pageObject.closeDialog);//关闭对话框
    },
    showDialog: function () {
        pageJs.showDialog('addDialogWrap');
    },
    closeDialog: function () {
        pageJs.closeDialog(function () {
            $('.需要清理数据的控件样式名').val('');
        });
    },
    initDialog: function () {//初始对话框必要元素
        pageObject.isModify = false;
       
    },
    initAddDialog: function () {//为添加书对话框添加必要元素
        pageObject.isModify = false;
        pageObject.showDialog();
    },
    initModifyDialog: function () {//为修改对话框添加必要元素，初始化要修改的数据
        pageObject.isModify = true;
        var infoid = $(this).attr('infoId');
        pageJs.loadAjaxFun(pageObject.getDetailUrl, { infoId: infoid}, function (data) {
            if (data.ActionResult) {
                //成功后
                for (items in data.Data){//遍历对象属性进行赋值
                    $('#' + items).val(data.Data[items]);
                }
                pageObject.showDialog();
            } else {
                //操作失败
            };
        });       
    },
    modifyInitPamater: function () {//获取修改要提交的参数
        for (items in pageObject.modfiyPamater) {//遍历对象属性进行赋值
            pageObject.modfiyPamater[items] = $('#' + items).val();
        };
    },
    addInitPamater: function () {//获取添加要提交的参数
        pageObject.addPamater = {$3};
    },
    infoSubmit: function () {//提交对话框数据
        if (pageObject.isModify) {
            pageObject.modifyInitPamater();
            pageObject.submitPamater = pageObject.modfiyPamater;
            pageObject.submitUrl = pageObject.modifySubmitUrl;
        }
        else {
            pageObject.addInitPamater();
            pageObject.submitPamater = pageObject.addPamater;
            pageObject.submitUrl = pageObject.addSubmitUrl;
        }
        pageJs.loadAjaxFun(pageObject.submitUrl, pageObject.submitPamater, function (data) {
            if (data.ActionResult) {
                //成功后
                pageObject.getPagerList();
                pageObject.closeDialog();
            } else {
                //操作失败
            };
        });
    },
    getPrePage: function () {
        if (pageObject.pagerPamater.currentPage > 1) {
            pageObject.pagerPamater.currentPage = pageObject.pagerPamater.currentPage - 1;
            pageObject.getPagerList();
        }
    },
    getNextPage: function () {
        pageObject.pagerPamater.currentPage = pageObject.pagerPamater.currentPage + 1;
        pageObject.getPagerList();
    },
    getPagerList: function () {
        pageJs.loadAjaxFun(pageObject.pagerUrl, pageObject.pagerPamater, function (data) {
            if (data.ActionResult) {
                var listHtml = '<li>{$1}';
                var temp;
                if (data.Data.length > 0) {
                    pageObject.listWrap.html('');
                    for (var i = 0; i < data.Data.length; i++) {
                        temp = listHtml.replace('$n', data.Data[i].N);
                        {$2}
                        pageObject.listWrap.append(temp);
                        pageObject.listWrap.append('<span><a class=\'modifyShow\' infoId=' + data.Data[i].Id + ' href=\'javascript:void(0)\'>编辑</a></span></li>');    }                pageObject.modifyShowDialogBtn = $('.modifyShow');//获取列表对象               
                    pageObject.modifyShowDialogBtn.live('click', pageObject.initModifyDialog);//修改对话框展示事件绑定  
                    pageObject.total = data.Total; 
                }else
                {
                    pageObject.pagerPamater.currentPage = pageObject.pagerPamater.currentPage - 1;
                }
               
            }
            else{
                alert('请求无数据')
                pageObject.pagerPamater.currentPage = pageObject.pagerPamater.currentPage - 1;
            }
        });
    }
};